import { ActionContext } from 'vuex'
import * as $api from '@/api/system'
import * as $model from '@/api/model/systemModel'
import { IStore } from '../types'
import {
  operatorSave,
  operatorDetail,
  operatorList,
  roleList,
  getSystemRleSave,
  getSystemRleDelete
} from '@/api/system'
export type ISystemState = {}

const state = {}

const mutations = {}

const actions = {
  // 获取字典表列表数据
  async getSystemDictionary({}: ActionContext<ISystemState, IStore>, params: $model.SystemDictionaryParams) {
    return await $api.getSystemDictionary(params)
  },
  // 获取字典表详情数据
  async getSystemDictionaryDetail({}: ActionContext<ISystemState, IStore>, params: $model.SystemDictionaryParams) {
    return await $api.getSystemDictionaryDetail(params)
  },
  // 删除字典表详情数据
  async removeSystemDictionary({}: ActionContext<ISystemState, IStore>, params: $model.SystemDictionaryParams) {
    return await $api.removeSystemDictionary(params)
  },
  // 获取字典表详情数据
  async setSystemDictionaryDetail(
    {}: ActionContext<ISystemState, IStore>,
    params: $model.SystemDictionaryDetailParams
  ) {
    return await $api.setSystemDictionaryDetail(params)
  },
  async operatorSave({}: ActionContext<ISystemState, IStore>, params: any) {
    return await operatorSave(params)
  },
  async operatorDetail({}: ActionContext<ISystemState, IStore>, params: any) {
    return await operatorDetail(params)
  },
  async operatorList({}: ActionContext<ISystemState, IStore>, params: any) {
    return await operatorList(params)
  },
  async roleList({}: ActionContext<ISystemState, IStore>, params: any) {
    return await roleList(params)
  },
  // 获取资源列表
  getSystemAuthority({}: ActionContext<ISystemState, IStore>, params: $model.SystemAuthorityParams) {
    return new Promise((resolve, reject) => {
      $api
        .getSystemAuthority(params)
        .then((res) => {
          // 格式化数据，数据结构适配ui组件，并且增加对应关联的父级id parentIds
          const initData = (list: any, parent: any) => {
            return list.map((item: any) => {
              if (item.children && item.children.length) {
                const data = {
                  ...item,
                  label: item.resourceDescription,
                  key: item.id,
                  value: item.id,
                  parentIds: parent ? [...(parent.parentIds || []), parent.id] : []
                }
                data.children = initData(item.children, data)
                return data
              } else {
                delete item.children
                return {
                  ...item,
                  label: item.resourceDescription,
                  key: item.id,
                  value: item.id,
                  parentIds: parent ? [...(parent.parentIds || []), parent.id] : []
                }
              }
            })
          }
          res.data = initData(res.data || [], null)
          resolve(res)
        })
        .catch(() => {
          reject()
        })
    })
  },
  // 编辑资源
  async setSystemAuthority({}: ActionContext<ISystemState, IStore>, params: $model.SystemAuthorityDetailParams) {
    return await $api.setSystemAuthority(params)
  },
  // 删除资源项
  async removeSystemAuthority({}: ActionContext<ISystemState, IStore>, params: $model.SystemAuthorityDetailParams) {
    return await $api.removeSystemAuthority(params)
  },
  // 角色/版本列表
  async getSystemRle({}: ActionContext<ISystemState, IStore>, params: $model.SystemRoleParams) {
    return new Promise((resolve, reject) => {
      $api
        .getSystemRle(params)
        .then((res: any) => {
          const data = res.data || []
          res.data = data.map((item: { roleName: string; id: number }) => {
            return {
              ...item,
              label: item.roleName,
              key: item.id,
              value: item.id
            }
          })
          resolve(res)
        })
        .catch(() => {
          reject()
        })
    })
  },
  // 默认岗位详情
  async getSystemRleDetail({}: ActionContext<ISystemState, IStore>, params: $model.SystemRoleParams) {
    return await $api.getSystemRleDetail(params)
  },
  // 保存默认岗位
  async getSystemRleSave({}: ActionContext<ISystemState, IStore>, params: $model.SystemRoleParams) {
    return await $api.getSystemRleSave(params)
  },
  // 保存默认岗位
  async getSystemRleDelete({}: ActionContext<ISystemState, IStore>, params: $model.SystemRoleParams) {
    return await $api.getSystemRleDelete(params)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
